const BaseController = require('./base_controller.js');
const CashierService = require('../service/cashier_service.js');

class CashierController extends BaseController {

  /**
   * 生成收款二维码
   */
  async generatePaymentQR() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      amount: 'required|int|min:1|name=收款金额',
      paymentMethod: 'required|string|in:wechat,alipay|name=支付方式',
      storeId: 'required|string|name=门店ID',
      description: 'string|max:100|name=收款描述',
    };

    let input = this.validateData(rules);

    try {
      const service = new CashierService();
      const result = await service.generatePaymentQR(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 确认支付
   */
  async confirmPayment() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      cashierId: 'required|string|name=收银记录ID',
      transactionId: 'required|string|name=交易ID',
    };

    let input = this.validateData(rules);

    try {
      const service = new CashierService();
      const result = await service.confirmPayment(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 获取收银记录
   */
  async getCashierLogs() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      page: 'int|min:1|default=1|name=页码',
      size: 'int|min:1|max:50|default=20|name=每页数量',
      storeId: 'string|name=门店ID',
      status: 'int|name=支付状态',
      dateRange: 'string|name=日期范围',
    };

    let input = this.validateData(rules);

    try {
      const service = new CashierService();
      const result = await service.getCashierLogs(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }
}

module.exports = CashierController;
